using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using CSP.SimpleCMS.Data;
using CSP.MembershipInterface.ObjectModel;

namespace CSP.SimpleCMS.DataModel
{
    /// <summary>Manages the operations related with the CaseStatus</summary>
    public static class CaseStatusDM
    {

        /// <summary>Returns a Dataset with the list of CaseStatus related with a CaseType</summary>
        /// <param name="caseTypeID">The integer which identifies the CaseType </param>
        /// <returns>A Dataset with all the CaseStatus information related with a CaseType</returns>
        public static DataSet GetCaseStatusesByCaseType(int caseTypeID)
        {
            DataSet dataSet = new DataSet();

            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPCaseStatusGET, new SqlParameter[] { new SqlParameter(DMConstants.FieldCaseTypeID, caseTypeID) });
            }

            return dataSet;
        }

        /// <summary>Returns an array with all the CaseStatus stored in the data source</summary>
        /// <returns>An array of CaseStatus</returns>      
        public static CaseStatus[] GetCaseStatus()
        {
            List<CaseStatus> caseStatus = new List<CaseStatus>();

            DataSet dataSet = new DataSet();
            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPCaseStatusGET, DMConstants.FieldCaseStatusID, null);
            }

            if (dataSet != null && dataSet.Tables.Count == 1 && dataSet.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in dataSet.Tables[0].Rows)
                {
                    CaseStatus crmCaseStatus = ParseRow(row);
                    caseStatus.Add(crmCaseStatus);
                }
            }

            return caseStatus.ToArray();
        }

        /// <summary>Returns a CaseStatus with the ID sent by parameter</summary>
        /// <param name="caseStatusID">The ID of the CaseStatus to be found</param>
        /// <returns>A CaseStatus object</returns>     
        public static CaseStatus GetCaseStatusByCaseStatusID(int caseStatusID)
        {
            CaseStatus crmCaseStatus=null;
            DataSet dataSet = new DataSet();
            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPCaseStatusGET, DMConstants.FieldCaseStatusID,caseStatusID);
            }

            if (dataSet != null && dataSet.Tables.Count == 1 && dataSet.Tables[0].Rows.Count > 0)
            {                
                    crmCaseStatus = ParseRow(dataSet.Tables [0].Rows [0]);                            
            }

            return crmCaseStatus;
        }


        /// <summary>Parses a datarow to a CaseStatus</summary>
        /// <param name="row">The datarow to be parsed</param>
        /// <returns>A CaseStatus object with the information stored in the datarow</returns>       
        private static CaseStatus ParseRow(DataRow row)
        {
            CaseStatus crmCaseStatus = new CaseStatus();

            crmCaseStatus.CaseStatusId = Convert.ToInt32 (row[DMConstants.FieldCaseStatusID]);
            crmCaseStatus.CaseStatusTitle = row[DMConstants.FieldCaseStatusTitle].ToString();


            return crmCaseStatus;
        }
    }
}
